package com.mark.web.stars.controller;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.mark.common.CurrentUser;
import com.mark.common.ServerResponse;
import com.mark.common.exception.Campuso2oException;
import com.mark.web.stars.entity.from.AddMemoFrom;
import com.mark.web.stars.entity.from.EditMemoFrom;
import com.mark.web.stars.entity.po.Memo;
import com.mark.web.stars.entity.vo.MemoVo;
import com.mark.web.stars.service.IMemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.*;

/**
 * @Description: 备忘录-控制器
 * @Author: Mark
 * @CreateDate: 2019/3/28 21:16
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Api(tags = "星运-备忘录模块")
@RestController
@RequestMapping(value = "/stars/memo/")
public class MemoController {

    private final IMemoService iMemoService;

    @Autowired
    public MemoController(IMemoService iMemoService) {
        this.iMemoService = iMemoService;
    }

    @ApiOperation(value = "添加备忘录")
    @PostMapping(value = "add")
    public ServerResponse<String> add(@Valid AddMemoFrom addMemoFrom, BindingResult result){
        if(result.hasErrors()){
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        Memo memo = new Memo();
        BeanUtils.copyProperties(addMemoFrom,memo);
        memo.setExpectTime(new Date(addMemoFrom.getExpectTime()));
        int currentUserId = CurrentUser.getCurrentUserId();
        memo.setFkUserId(currentUserId);
        return iMemoService.add(memo);
    }

    @ApiOperation(value = "获取搜索条件内的备忘录信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前的页数",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",defaultValue = "50")
    })
    @PostMapping(value = "getMemoListBySearch")
    public ServerResponse<PageInfo<MemoVo>> getMemoListBySearch(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                                @RequestParam(value = "pageSize",defaultValue = "50") int pageSize,
                                                                @RequestParam(value = "status" ,defaultValue = "0") int status,
                                                                @RequestParam(value = "searchTime") Long searchTime){
        //搜索备忘录状态的校验
        List<Integer> statusList = Arrays.asList(0, 1, 2, 3);
        if(!statusList.contains(status)){
            throw new Campuso2oException("输入的状态不正确！");
        }

        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("pageNum",pageNum);
        paramMap.put("status",status);
        paramMap.put("pageSize",pageSize);
        paramMap.put("searchTime",searchTime);
        paramMap.put("currentUserId",CurrentUser.getCurrentUserId());
        return  iMemoService.getMemoListBySearch(paramMap);
    }

    @ApiOperation(value = "编辑备忘录")
    @PostMapping(value = "edit")
    public ServerResponse<String> edit(@Valid EditMemoFrom editMemoFrom, BindingResult result){
        if(result.hasErrors()){
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        Memo memo = new Memo();
        BeanUtils.copyProperties(editMemoFrom,memo);
        memo.setFkUserId(CurrentUser.getCurrentUserId());
        memo.setExpectTime(new Date(editMemoFrom.getExpectTime()));
        return iMemoService.edit(memo);
    }

    @ApiOperation(value = "完成备忘录")
    @PostMapping(value = "finish")
    public ServerResponse<String> finish(Integer id){
        if(id == null || id < 0){
            return ServerResponse.createByErrorMessage("输入id不正确！");
        }
        return iMemoService.finish(id,CurrentUser.getCurrentUserId());
    }

    @ApiOperation(value = "删除备忘录")
    @PostMapping(value = "delete")
    public ServerResponse<String> delete(Integer id){
        if(id == null || id < 0){
            return ServerResponse.createByErrorMessage("输入id不正确！");
        }
        return iMemoService.delete(id,CurrentUser.getCurrentUserId());
    }

}
